//
//  AlarmCardsView.swift
//  SBSafetyApp
//
//  Created by Lina on 2023/4/22.
//

import Foundation
import UIKit

enum AlarmType: String {
    case robbery = "0" // 劫警
    case burglary = "1" //  盗警
    case fire = "2" // 火警
    case electricity = "3" // 电
    case shock = "4" // 震动
    case unalarm = "5" // 撤防
    case alarm = "6" // 布防
}

class AlarmCardsView: UIScrollView {
    var records: [Any?] = []
    
    var deptId: Int64?
    var deptName: String?
    
    init(deptId: Int64?, deptName: String?) {
        super.init(frame: .zero)
        setupUI()
        self.deptId = deptId
        self.deptName = deptName
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func updateUI(withModels models: [AlarmCountByTypeModal?]) {
        removeAllSubViews()
        
        let width = (ScreenWidth - 76) / 5.0
        var last: AlarmCardView?
        for (index, modal) in models.enumerated() {
            let v = AlarmCardView()
            v.updateUI(withModal: modal)
            v.handleWith = { [self] () -> () in
                let vc = AlarmTypeStatisticsVC(withAlarmType: modal?.dictValue ?? "", name: modal?.dictLabel ?? "", deptId: deptId, deptName: deptName)
                getFirstViewController()?.navigationController?.pushViewController(vc, animated: true)
            }
            addSubview(v)
            v.snp.makeConstraints { make in
                make.width.equalTo(width)
                make.height.equalTo(80)
                make.top.equalToSuperview().offset(12)
                if last == nil {
                    make.left.equalToSuperview().offset(10)
                } else {
                    make.left.equalTo(last!.snp.right).offset(9)
                }
                
                if index == models.count - 1 {
                    make.right.equalToSuperview().offset(-10)
                }
            }
            last = v
        }
    }
    
    func setupUI() {
        backgroundColor = .white
        layer.cornerRadius = 10
        layer.masksToBounds = true
    }
}

class AlarmCardView: UIControl {
    let bgIV = UIImageView()
    let iconIV = UIImageView()
    let titleL = UILabel()
    let numL = UILabel()
    
    var model: AlarmCountByTypeModal?

    open var handleWith:(() -> ())?

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
        addTarget(self, action: #selector(handleTypeStatistics), for: .touchUpInside)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func handleTypeStatistics() {
        handleWith?()
    }
    
    func updateUI(withModal modal: AlarmCountByTypeModal?) {
        self.model = modal
        titleL.text = modal?.dictLabel
        numL.text = "总共(\(modal?.total ?? 0))"

        switch AlarmType(rawValue: modal?.dictValue ?? "") {
        case .robbery:
            bgIV.image = UIImage(named: "ic_alarm_bg1")
            iconIV.image = UIImage(named: "ic_alarm_1")
        case .burglary:
            bgIV.image = UIImage(named: "ic_alarm_bg2")
            iconIV.image = UIImage(named: "ic_alarm_2")
        case .fire:
            bgIV.image = UIImage(named: "ic_alarm_bg3")
            iconIV.image = UIImage(named: "ic_alarm_3")
        case .electricity:
            bgIV.image = UIImage(named: "ic_alarm_bg4")
            iconIV.image = UIImage(named: "ic_alarm_4")
        case .shock:
            bgIV.image = UIImage(named: "ic_alarm_bg5")
            iconIV.image = UIImage(named: "ic_alarm_5")
        default:
            bgIV.image = UIImage(named: "ic_alarm_bg5")
            iconIV.image = UIImage(named: "ic_alarm_5")
            break
        }
    }

    func setupUI() {
        layer.masksToBounds = true
        
        addSubview(bgIV)
        bgIV.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        iconIV.contentMode = .scaleAspectFit
        addSubview(iconIV)
        iconIV.snp.makeConstraints { make in
            make.height.equalTo(22)
            make.top.equalToSuperview().offset(8)
            make.centerX.equalToSuperview()
        }
        
        titleL.textAlignment = .center
        titleL.textColor = .white
        titleL.font = .systemFont(ofSize: 14)
        addSubview(titleL)
        titleL.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(2)
            make.right.equalToSuperview().offset(-2)
            make.top.equalTo(iconIV.snp.bottom).offset(6)
        }
        
        numL.textAlignment = .center
        numL.textColor = .white
        numL.font = .systemFont(ofSize: 12)
        addSubview(numL)
        numL.snp.makeConstraints { make in
            make.top.equalTo(titleL.snp.bottom).offset(8)
            make.left.equalTo(titleL.snp.left)
            make.right.equalTo(titleL.snp.right)
        }
    }
}
